package com.yape.stockService.impl;

import com.yape.stockService.INewStockService;
import com.yape.dao.INewStockDao;
import com.yape.po.NewStockListing;
import com.yape.vo.NewStockListingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yape
 * @description
 * @date 2025/5/24 21:48
 */
@Service
@Slf4j
public class NewStockService implements INewStockService {

    @Resource
    private INewStockDao newStockDao;

    @Override
    public NewStockListingVO queryNewStockListing(int currentPage, int pageSize) {
        // 总记录数
        int count = newStockDao.queryElementsCount();
        // 记录列表
        List<NewStockListing> elements = newStockDao.queryElements((currentPage - 1) * pageSize, pageSize);

        NewStockListingVO newStockListingVO = NewStockListingVO.builder()
                .totalElements(count)
                .totalPages((count + pageSize - 1) / pageSize)
                .currentPage(currentPage)
                .pageSize(pageSize)
                .build();
        // 转换为 VO 内部类
        List<NewStockListingVO.NewStock> content = elements.stream()
                .map(e -> NewStockListingVO.NewStock.builder()
                        .id(e.getId())
                        .securityName(e.getSecurityName())
                        .listingDate(e.getListingDate())
                        .build())
                .toList();

        newStockListingVO.setContent(content);
        return newStockListingVO;
    }
}
